package com.dchh.android.suverynew.module.local.impl;

import com.dchh.android.suverynew.common.SPreferencesManager;
import com.dchh.android.suverynew.module.local.ICycleSelectMoudle;
import com.dhcc.android.base.base.MSApplication;
import com.dhcc.android.base.common.bean.Period;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.dataparse.DataParse;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by eric on 2016/8/11.
 */
public class CycleSelectMoudle implements ICycleSelectMoudle {
    private ICycleSelectMoudleListener mCycleSelectMoudleListener;

    public CycleSelectMoudle(ICycleSelectMoudleListener mCycleSelectMoudleListener) {
        this.mCycleSelectMoudleListener = mCycleSelectMoudleListener;
    }

    @Override
    public void initData(InputStream inputStream) {
        List<Period> plist = DataParse.dataParse(inputStream, Period.class, "period");
        List<Map<String, String>> list = new ArrayList<>();
        for (int i = 0; i < plist.size(); i++) {
            Period pp = plist.get(i);
            Map<String, String> m = new HashMap<String, String>();

            m.put("id", pp.getCode());// 调查周期类型代码
            m.put("name", pp.getLabel());// 标注
            // 2.将解析完的数据添加到list
            list.add(m);
        }
        mCycleSelectMoudleListener.onDataReturn(list);
    }

    @Override
    public void restore() {
        // 异常关闭
        boolean ef = SPreferencesManager.getValue(MSApplication.EXCEPTION_FLAG, false);
        if (ef) {
            String code = SPreferencesManager.getValue(MSApplication.PERIOD_CODE, "");

            String label = SPreferencesManager.getValue(MSApplication.PERIOD_LABEL, "");

            System.out.println("###恢复：" + code + "--" + label);

            Period p = new Period();
            p.setCode(code);
            p.setLabel(label);
            Constants.currentCycle = p;
            mCycleSelectMoudleListener.onRestoreData(Constants.currentCycle);
        }
    }

    public interface ICycleSelectMoudleListener {
        void onDataReturn(List<Map<String, String>> list);

        void onRestoreData(Period p);
    }
}
